/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package plan.mobile;

import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import mobile.Call;
import mobile.Message;
import mobile.MobileConsumption;
import plan.Plan;
import turnover.Consumption;

/**
 *
 * @author Ale
 */
public class Yoigo_la_del_0 extends Plan {

    private double startQuote = 0d;
    private double minuteNationalCost = 0.12d;
    private double minuteInternationalCost = 0.30d;
    private double messageCost = 0.08d;

    public Yoigo_la_del_0() {
        super.setName("Yoigo la del 0");
    }

    public double getStartQuote() {
        return startQuote;
    }

    @Override
    public double getCost(Consumption consumption) {

        double total = 0d;
        MobileConsumption mc = (MobileConsumption) consumption;
        Iterator it = mc.getCallList().listIterator();
        List<Long> numList = new LinkedList();
        long durationMax = 60 * 60;
        long duration = 0;
        while (it.hasNext()) {
            Call c = (Call) it.next();
            if (c.isSpecial()) {
                total += c.getCost();
            } else if (this.isYoigo(c) && !numList.isEmpty() && !this.overDurationMax(durationMax, duration) && this.isCallDay(c)) {
                duration += c.getDuration().changeSeconds();
                numList.add(c.getMobile());
            } else {
                duration += c.getDuration().changeSeconds();
                total += c.getDuration().changeSeconds() * this.minuteNationalCost / 60;
                numList.add(c.getMobile());
            }
        }
        it = mc.getMessList().listIterator();
        while (it.hasNext()) {
            Message m = (Message) it.next();
            if (m.isSpecial()) {
                total += m.getCost();
            } else {
                total += this.messageCost;
            }
        }
        if (total < 6d) {
            total = 6d;
        }
        total += total * 0.16d;
        return total;
    }

    public double getMessageCost() {
        return messageCost;
    }

    public double getMinuteCost() {
        return minuteNationalCost;
    }
    
    private boolean overDurationMax(long durationMax, long duration) {
        return durationMax <= duration;
    }

    private boolean isYoigo(Call c) {
        return (c.getOperador().getName().equals("Yoigo"));

    }

    public boolean containsCallDay(List<Long> numList) {
        return (!numList.isEmpty());
    }

    private boolean isCallDay(Call c) {
        boolean res = false;
        java.util.Date fecha = new Date();
        util.Date d = c.getDate();
        int year = d.getYear();
        int month = d.getMonth();
        int day = d.getDay();
        int dia = fecha.compareTo(new Date(year, month, day));
        if (dia == 0) {
            res = true;
        }
        return res;

    }
}
